#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include "bas/bas_logger.h"
#include "tst/test_frame.h"

int tst_init(void)
{
    if(system("rm -f file*log*"));
    return 0;
}

int tst_logStderr(void)
{
    LOG_INFO("test log info");
    LOG_INFO_PLAIN("test log info");
    LOG_FATAL("test log fatal");
    LOG_FATAL_PLAIN("test log fatal");
    LOG_ERROR("test log error");
    LOG_ERROR_PLAIN("test log error");
    LOG_WARN("test log warn");
    LOG_WARN_PLAIN("test log warn");
    LOG_DEBUG("test log debug");
    LOG_DEBUG_PLAIN("test log debug");

    return 0;
}

int tst_logFile(void)
{
    T_Logger *ptLogger = NULL;
    
    TEST_ASSERT((ptLogger = basLoggerInit("file.log", 0, false)) != NULL);

    LOG_INFO_HANDLE(ptLogger, "test log info");
    LOG_FATAL_HANDLE(ptLogger, "test log fatal");
    LOG_ERROR_HANDLE(ptLogger, "test log error");
    LOG_WARN_HANDLE(ptLogger, "test log warn");
    LOG_DEBUG_HANDLE(ptLogger, "test log debug");

    LOG_INFO_HANDLE_PLAIN(ptLogger, "test log info");
    LOG_FATAL_HANDLE_PLAIN(ptLogger, "test log fatal");
    LOG_ERROR_HANDLE_PLAIN(ptLogger, "test log error");
    LOG_WARN_HANDLE_PLAIN(ptLogger, "test log warn");
    LOG_DEBUG_HANDLE_PLAIN(ptLogger, "test log debug");

    return 0;
}

int tst_logFileWithChangeSize(void)
{
    T_Logger *ptLogger = NULL;

    TEST_ASSERT((ptLogger = basLoggerInit("fileChangSize.log", 10, false)) != NULL);

    for (int i = 0; i < 100000; ++i)
    {
        LOG_INFO_HANDLE(ptLogger, "test log info");
        LOG_FATAL_HANDLE(ptLogger, "test log fatal");
        LOG_ERROR_HANDLE(ptLogger, "test log error");
        LOG_WARN_HANDLE(ptLogger, "test log warn");
        LOG_DEBUG_HANDLE(ptLogger, "test log debug");
    }

    return 0;
}

int tst_logFileWithDateChange(void)
{
    T_Logger *ptLogger = NULL;

    TEST_ASSERT((ptLogger = basLoggerInit("fileDateChang.log", 0, true)) != NULL);
    
    for (;;)
    {
        LOG_INFO_HANDLE(ptLogger, "test log info");
        LOG_FATAL_HANDLE(ptLogger, "test log fatal");
        LOG_ERROR_HANDLE(ptLogger, "test log error");
        LOG_WARN_HANDLE(ptLogger, "test log warn");
        LOG_DEBUG_HANDLE(ptLogger, "test log debug");
        //sleep(1);
    }

    return 0;
}

int tst_logLevel(void)
{
    T_Logger *ptLogger = NULL;

    TEST_ASSERT((ptLogger = basLoggerInit("fileLogLevel.log", 0, false)) != NULL);

    basLoggerSetLogLevel(ptLogger, -1);
    LOG_INFO_HANDLE_PLAIN(ptLogger, "LOG_LEVEL_SET_TO_INVALID[-1][%d]", basLoggerGetLogLevel(ptLogger));
    LOG_INFO_HANDLE(ptLogger, "test log info");
    LOG_FATAL_HANDLE(ptLogger, "test log fatal");
    LOG_ERROR_HANDLE(ptLogger, "test log error");
    LOG_WARN_HANDLE(ptLogger, "test log warn");
    LOG_DEBUG_HANDLE(ptLogger, "test log debug");
    LOG_INFO_HANDLE_PLAIN(ptLogger, "\n");

    basLoggerSetLogLevel(ptLogger, LOGLEVEL_INFO);
    LOG_INFO_HANDLE_PLAIN(ptLogger, "LOG_LEVEL_SET_TO_INFO[%d]", basLoggerGetLogLevel(ptLogger));
    LOG_INFO_HANDLE(ptLogger, "test log info");
    LOG_FATAL_HANDLE(ptLogger, "test log fatal");
    LOG_ERROR_HANDLE(ptLogger, "test log error");
    LOG_WARN_HANDLE(ptLogger, "test log warn");
    LOG_DEBUG_HANDLE(ptLogger, "test log debug");
    LOG_INFO_HANDLE_PLAIN(ptLogger, "\n");

    basLoggerSetLogLevel(ptLogger, LOGLEVEL_FATAL);
    LOG_INFO_HANDLE_PLAIN(ptLogger, "LOG_LEVEL_SET_TO_FATAL[%d]", basLoggerGetLogLevel(ptLogger));
    LOG_INFO_HANDLE(ptLogger, "test log info");
    LOG_FATAL_HANDLE(ptLogger, "test log fatal");
    LOG_ERROR_HANDLE(ptLogger, "test log error");
    LOG_WARN_HANDLE(ptLogger, "test log warn");
    LOG_DEBUG_HANDLE(ptLogger, "test log debug");
    LOG_INFO_HANDLE_PLAIN(ptLogger, "\n");

    basLoggerSetLogLevel(ptLogger, LOGLEVEL_ERROR);
    LOG_INFO_HANDLE_PLAIN(ptLogger, "LOG_LEVEL_SET_TO_ERROR[%d]", basLoggerGetLogLevel(ptLogger));
    LOG_INFO_HANDLE(ptLogger, "test log info");
    LOG_FATAL_HANDLE(ptLogger, "test log fatal");
    LOG_ERROR_HANDLE(ptLogger, "test log error");
    LOG_WARN_HANDLE(ptLogger, "test log warn");
    LOG_DEBUG_HANDLE(ptLogger, "test log debug");
    LOG_INFO_HANDLE_PLAIN(ptLogger, "\n");

    basLoggerSetLogLevel(ptLogger, LOGLEVEL_WARN);
    LOG_INFO_HANDLE_PLAIN(ptLogger, "LOG_LEVEL_SET_TO_WARN[%d]", basLoggerGetLogLevel(ptLogger));
    LOG_INFO_HANDLE(ptLogger, "test log info");
    LOG_FATAL_HANDLE(ptLogger, "test log fatal");
    LOG_ERROR_HANDLE(ptLogger, "test log error");
    LOG_WARN_HANDLE(ptLogger, "test log warn");
    LOG_DEBUG_HANDLE(ptLogger, "test log debug");
    LOG_INFO_HANDLE_PLAIN(ptLogger, "\n");

    basLoggerSetLogLevel(ptLogger, LOGLEVEL_DEBUG);
    LOG_INFO_HANDLE_PLAIN(ptLogger, "LOG_LEVEL_SET_TO_DEBUG[%d]", basLoggerGetLogLevel(ptLogger));
    LOG_INFO_HANDLE(ptLogger, "test log info");
    LOG_FATAL_HANDLE(ptLogger, "test log fatal");
    LOG_ERROR_HANDLE(ptLogger, "test log error");
    LOG_WARN_HANDLE(ptLogger, "test log warn");
    LOG_DEBUG_HANDLE(ptLogger, "test log debug");
    LOG_INFO_HANDLE_PLAIN(ptLogger, "\n");

    basLoggerSetLogLevel(ptLogger, 100);
    LOG_INFO_HANDLE_PLAIN(ptLogger, "LOG_LEVEL_SET_TO_INVALID[100][%d]", basLoggerGetLogLevel(ptLogger));
    LOG_INFO_HANDLE(ptLogger, "test log info");
    LOG_FATAL_HANDLE(ptLogger, "test log fatal");
    LOG_ERROR_HANDLE(ptLogger, "test log error");
    LOG_WARN_HANDLE(ptLogger, "test log warn");
    LOG_DEBUG_HANDLE(ptLogger, "test log debug");
    LOG_INFO_HANDLE_PLAIN(ptLogger, "\n");

    return 0;
}

int tst_memLeak(void)
{
    T_Logger *ptLogger = NULL;

    for (;;)
    {
        TEST_ASSERT((ptLogger = basLoggerInit("fileLogmemLeak.log", 0, false)) != NULL);
        basLoggerClose(ptLogger);
    }
    return 0;
}

int tst_logDefault(void)
{
    T_Logger *ptLogger = NULL;
    T_Logger *ptOldLogger = NULL;

    TEST_ASSERT((ptLogger = basLoggerInit("fileLogDefault.log", 0, false)) != NULL);

    ptOldLogger = basLoggerSetDefault(ptLogger);
    LOG_INFO("test log info");
    LOG_INFO_PLAIN("test log info");
    LOG_FATAL("test log fatal");
    LOG_FATAL_PLAIN("test log fatal");
    LOG_ERROR("test log error");
    LOG_ERROR_PLAIN("test log error");
    LOG_WARN("test log warn");
    LOG_WARN_PLAIN("test log warn");
    LOG_DEBUG("test log debug");
    LOG_DEBUG_PLAIN("test log debug");

    ptOldLogger = basLoggerSetDefault(ptOldLogger);
    basLoggerClose(ptOldLogger);

    LOG_INFO("test log info");
    LOG_INFO_PLAIN("test log info");
    LOG_FATAL("test log fatal");
    LOG_FATAL_PLAIN("test log fatal");
    LOG_ERROR("test log error");
    LOG_ERROR_PLAIN("test log error");
    LOG_WARN("test log warn");
    LOG_WARN_PLAIN("test log warn");
    LOG_DEBUG("test log debug");
    LOG_DEBUG_PLAIN("test log debug");

    basLoggerSetLogLevel(basLoggerGetDefault(), LOGLEVEL_INFO);

    LOG_INFO("test log info");
    LOG_INFO_PLAIN("test log info");
    LOG_FATAL("test log fatal");
    LOG_FATAL_PLAIN("test log fatal");
    LOG_ERROR("test log error");
    LOG_ERROR_PLAIN("test log error");
    LOG_WARN("test log warn");
    LOG_WARN_PLAIN("test log warn");
    LOG_DEBUG("test log debug");
    LOG_DEBUG_PLAIN("test log debug");

    return 0;
}
